package com.smart.parking.service.impl;


import com.smart.parking.service.PriceService;
import com.smart.parking.utils.PageUtils;
import com.smart.parking.utils.Query;
import com.smart.parking.vo.PriceVO;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.smart.parking.mapper.PriceMapper;
import com.smart.parking.entity.PriceEntity;


@Service("priceService")
public class PriceServiceImpl extends ServiceImpl<PriceMapper, PriceEntity> implements PriceService {

    @Autowired
    private Mapper dozerMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PriceEntity> page = this.page(
                new Query<PriceEntity>().getPage(params),
                new QueryWrapper<PriceEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 计算价格
     *
     * @param duration 停车时长
     * @return double
     */
    @Override
    public double calculationPrice(long duration) {
        //获取价格区间
        List<PriceEntity> priceEntityList = baseMapper.selectList(new QueryWrapper<PriceEntity>().le("upper_limit", duration));
        //价格
        final double[] sum = {0.0};
        final long[] price = {duration};
        //先排序，在计算价格
        List<PriceEntity> collect = priceEntityList.stream().sorted(Comparator.comparing(PriceEntity::getUpperLimit)).collect(Collectors.toList());
        //计算价格
        collect.forEach(priceEntity -> {
            if (duration > priceEntity.getUpperLimit()) {
                sum[0] += priceEntity.getUnitPrice() * priceEntity.getUpperLimit();
                price[0] -= priceEntity.getUpperLimit();
            } else {
                sum[0] += price[0] * priceEntity.getUnitPrice();
            }
        });
        return sum[0];
    }

    /**
     * 添加
     *
     * @param priceVO 价格签证官
     */
    @Override
    public void addPrice(PriceVO priceVO) {
        PriceEntity priceEntity = dozerMapper.map(priceVO, PriceEntity.class);
        baseMapper.insert(priceEntity);
    }

    /**
     * 批量添加
     *
     * @param priceVOList 价格volist
     */
    @Override
    public void addPriceList(List<PriceVO> priceVOList) {
        priceVOList.forEach(this::addPrice);
    }

    /**
     * 删除
     *
     * @param priceId 价格id
     */
    @Override
    public void deletePrice(Long priceId) {
        baseMapper.deleteById(priceId);
    }

    /**
     * 批量删除
     *
     * @param priceIds 价格id
     */
    @Override
    public void deletePrices(Long[] priceIds) {
        for (Long priceId : priceIds) {
            this.deletePrice(priceId);
        }
    }

    /**
     * 更新价格
     *
     * @param priceEntity 价格实体
     */
    @Override
    public void updatePrice(PriceEntity priceEntity) {
        baseMapper.updateById(priceEntity);
    }

}
